import { Image as VanImage } from "vant";
import { scrollMixin } from "utils/mixin";
export default {
  components: {
    VanImage
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      goodsList: [],
      query: {
        //筛选商品的
        page: 1,
        longitude: "",
        latitude: "",
        city: "",
        cityCode:'',
        travel_around_goods_type: 0,
        is_recommand: 0,
        category: 0,
        title: "",
        basic_info:1
      },
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    if ((this.$route.query.keyword ?? "") !== "") {
      this.query.title = this.$route.query.keyword.trim();
    }
    this.query.city = this.$route.query.city;
    this.query.latitude = this.$route.query.latitude;
    this.query.longitude = this.$route.query.longitude;
    this.query.cityCode = this.$route.query.cityCode;
    this.page = 1;
    let arr = localStorage.getItem("searchHistory") || [];
    let searchArr = [];
    if (arr.length > 0) {
      arr = JSON.parse(arr);
      let len = arr.length;
      if (len >= 10) {
        arr.pop();
      }
    }
    let timer = Date.parse(new Date());
    arr.unshift({ name: this.query.title, key: timer });
    searchArr = arr.filter((currentValue, currentIndex, selfArr) => {
      //去重
      return selfArr.findIndex(x => x.name === currentValue.name) === currentIndex;
    });
    localStorage.setItem("searchHistory", JSON.stringify(searchArr));
    this.getGoodsList();
    if(!this.$route.query.cityCode) {
      //获取定位的信息加载商品列表
      setTimeout(() => {
        this.fun
          .getLocation()
          .then(res => {
            let p = this.fun.bd_encrypt(res.point.lng, res.point.lat);
            this.query.latitude = p.lat;
            this.query.longitude = p.lng;
            this.city = res.addressComponent.city;
            this.getGoodsList();
          })
          .catch(err => {
            this.locationStatus = false;
            this.getGoodsList();
          });
      }, 2000);
    }
  },
  methods: {
    storeInput(event) {
      this.storeName = event;
    },
    goodsNavTo(id) {
      this.$router.push(this.fun.getUrl("tourAroundGoodsDetails", {}, { goodsId: id,latitude: this.query.latitude ,longitude:this.query.longitude }));
    },
    searchClick() {
      this.page = 1;
      //修复重复搜索时候，点击进去商品详情后，再返回时，总会搜索第一个
      if(this.$route.query.keyword == this.query.title) {
        this.getGoodsList();
      }else {
        const query = JSON.parse(JSON.stringify(this.$route.query))
        query.keyword = this.query.title;//title 修改的参数
        this.$router.push({ path: this.$route.path, query });
      }
    },
    getGoodsList() {
      if (this.query.latitude === "" && this.query.longitude === "") {
        this.locationMore = false;
      }
      $http.post("plugin.travel-around.frontend.goods.goods.getGoodsList", this.query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let basicInfo = response.basic_info;
          this.isLoadMore = true;
          this.total_page = res.last_page;
          if (!this.total_page) {
            this.query.page = 0;
          }
          this.goodsList = res.data;
          this.$nextTick(() => {
            this.fun.wxShare(
              "",
              { mid: this.fun.getKeyByMid() },
              {
                link: window.location.href,
                title: '周边游-'+basicInfo?.home.mailInfo?.name,
                imgUrl: basicInfo?.home.mailInfo?.logo_url,
                description: '全国周边吃喝玩乐特惠套餐！'
              }
            );
          },50);
        } else {
          this.query.page = this.query.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.query.page >= this.total_page) {
        return;
      } else {
        this.query.page = this.query.page + 1;
        $http.post("plugin.travel-around.frontend.goods.goods.getGoodsList", this.query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.goodsList = [...this.goodsList, ...myData]; //数组拼接
            } else {
              this.query.page = this.query.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  }
};
